<template>
  <div class="form-wrapper">
    <div class="block-header">
      <n-space>
        <div class="title">{{ $t("form list") }}</div>
        <n-select
          size="small"
          style="width: 180px"
          :options="sortOptions"
          v-model:value="searchParams.sort"
        ></n-select>
      </n-space>
      <n-space style="margin-left: auto">
        <n-input
          style="width: 150px"
          size="small"
          :placeholder="$t('please input keyword')"
        >
          <template #prefix>
            <n-icon :component="SearchOutline" />
          </template>
        </n-input>
        <n-button size="small" type="primary" secondary strong>{{
          $t("new form")
        }}</n-button>
      </n-space>
    </div>
    <div class="form-content">
      <div
        @click="goEditDetail(item.pid)"
        class="form-item"
        v-for="(item, index) in formList"
      >
        <div class="form-header">
          <n-space>
            <!-- <n-image class="img" :src="item.img"></n-image> -->
            <n-icon size="28px" class="img">
              <DocumentTextOutline></DocumentTextOutline>
            </n-icon>
            <n-space vertical>
              <div class="header-top">{{ item.name }}</div>
              <div class="desc">{{ item.createTime }}</div>
            </n-space>
          </n-space>
          <n-popselect
            :options="itemPopOptions"
            @update:value="handleItemOptions($event, item.id)"
            trigger="click"
          >
            <n-button
              @click.stop="handleformEdit"
              quaternary
              circle
              size="small"
            >
              <template #icon>
                <n-icon><EllipsisVerticalOutline /></n-icon>
              </template>
            </n-button>
          </n-popselect>
        </div>
        <div class="form-bottom">
          <n-space align="center">
            <n-avatar size="small" round :src="item.user.headImg"></n-avatar>
            <!-- <span class="desc">admin 5 {{ $t("minutes ago") }}</span> -->
            <span class="desc"
              >{{ item.user.nickName }} {{ formatPast(item.updateTime) }}</span
            >
          </n-space>
          <n-tag :bordered="false" type="primary">{{
            item.status == 1 ? $t("release") : $t("debug")
          }}</n-tag>
        </div>
      </div>
      <div @click="handleformAdd" class="form-item add">
        {{ $t("new form") }}
      </div>
    </div>
    <DialogForm
      :schema="addformJson"
      ref="formFormRef"
      @submit="handleformConfirm"
    ></DialogForm>
    <!-- <formForm ref="formFormRef"></formForm> -->
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import {
  SearchOutline,
  EllipsisVerticalOutline,
  DocumentTextOutline,
} from "@vicons/ionicons5";
import { addFormApi, deleteFormApi, getForms } from "@/api/form";
import { useRouter } from "vue-router";
// import formForm from "./form/addform.vue";
import DialogForm from "@/components/form-builder/dialog.vue";
import addformJson from "./form/add-form.json";
import { useI18n } from "vue-i18n";
import { formatPast } from "@/utils";
import { useMessage, useDialog } from "naive-ui";
import { v1 } from "uuid";
const message = useMessage();
const dialog = useDialog();
const { t } = useI18n();
const formFormRef = ref();
const router = useRouter();
const searchParams = reactive({
  sort: 0,
  page: 1,
  size: 100,
});
const formList = ref([]);
const sortOptions = ref([
  {
    label: t("create time newest"),
    value: 0,
  },
  {
    label: t("create time oldest"),
    value: 1,
  },
  {
    label: t("last open newest"),
    value: 2,
  },
  {
    label: t("last open oldest"),
    value: 3,
  },
]);
const itemPopOptions = ref([
  {
    label: "编辑",
    value: 0,
  },
  {
    label: "删除",
    value: 1,
  },
]);
const handleItemOptions = (e: number, index: number) => {
  console.log(e);
  if (e == 0) {
    const item = formList.value.find((item) => item.id == index);
    formFormRef.value.open({
      title: "编辑表单",
      data: item,
    });
  } else if (e == 1) {
    dialog.error({
      title: "删除表单",
      content: "删除后不可撤销",
      positiveText: "确定",
      negativeText: "取消",
      onPositiveClick(e) {
        deleteFormApi([index])
          .then((res) => {
            message.success("删除成功");
            getformList();
          })
          .catch((e) => {
            message.error("删除失败");
          });
      },
    });
  }
};
const getformList = () => {
  getForms(searchParams).then((res: any) => {
    console.log(res);
    formList.value = res.data.list;
  });
};
onMounted(() => {
  getformList();
});
const handleformEdit = () => {};
const handleformAdd = () => {
  formFormRef.value.open({
    title: "添加表单",
    data: {
      pid: v1(),
      img: "https://rcs-bucket.oss-cn-hangzhou.aliyuncs.com/app%2Fspace%2Fe96700463f8d4b448290b0aab1c0470f_3362.png",
    },
  });
};
const handleformConfirm = ({ title, data }) => {
  console.log(data);
  if (title == "添加表单") {
    addFormApi(data)
      .then((res) => {
        message.success("添加成功");
        getformList();
      })
      .catch((e) => {
        message.error(`添加失败:${e}`);
      });
  } else if (title == "编辑表单") {
    addFormApi(data)
      .then((res) => {
        message.success("编辑成功");
        getformList();
      })
      .catch((e) => {
        message.error(`编辑失败:${e}`);
      });
  }
};
const goEditDetail = (pid: string) => {
  router.push(`/form-detail?id=${pid}`);
};
</script>

<style lang="scss" scoped>
.form-wrapper {
  padding-left: 2px;
}
.block-header {
  display: flex;
  align-items: center;
  .title {
    font-weight: bold;
    font-size: 24px;
  }
}
.form-content {
  display: flex;
  margin: 24px 0;
  flex-wrap: wrap;
  .form-item {
    width: 240px;
    height: 140px;
    box-sizing: border-box;
    text-align: left;
    // background-color: rgb(28, 28, 31);
    background-color: #f2f3f5;
    border-radius: 4px;
    padding: 12px;
    margin-right: 24px;
    margin-bottom: 24px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    user-select: none;
    .form-header {
      display: flex;
      justify-content: space-between;
    }
    .form-bottom {
      margin-top: auto;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .img {
      width: 44px;
      height: 44px;
      border-radius: 4px;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      padding-bottom: 12px;
    }
    .desc {
      font-size: 12px;
      color: grey;
    }
  }
  .add {
    border: 1px dashed #63e2b7;
    background-color: #63e2b711;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .add:hover {
    background-color: #63e2b733 !important;
  }
  .form-item:hover {
    box-shadow: 0px 0px 0px 2px #63e2b799;
    // background-color: rgba(51, 51, 53, 0.8);
    // box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.1);
  }

  .form-item:active {
    // background-color: #63e2b711;
    // box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.1);
  }
}
.dark .form-item {
  background-color: rgb(28, 28, 31) !important;
}
</style>
